package cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.search.result;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import cz.muni.fi.pa165.musiclibrary.api.service.dto.IAlbumDTO;
import cz.muni.fi.pa165.musiclibrary.api.service.dto.IArtistDTO;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.album.AlbumPage;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.artist.ArtistPage;

/**
 *
 * @author Maros Kucbel
 * @date 2012-11-24T17:42:20+0100
 */
public final class AlbumsPanel extends Panel {

    private Map<IAlbumDTO, List<IArtistDTO>> albums;
    
    public AlbumsPanel(String id, Map<IAlbumDTO, List<IArtistDTO>> albums) {
        super(id);
        this.albums = albums;
    }

    @Override
    protected void onInitialize() {
        super.onInitialize();
        this.initComponents();
    }
    
    private void initComponents() {
        List<IAlbumDTO> albumList = new ArrayList<IAlbumDTO>(this.albums.keySet());
        ListView<IAlbumDTO> albumsView = new ListView<IAlbumDTO>(ComponentIDs.ALBUMS_VIEW, albumList) {

            @Override
            protected void populateItem(ListItem<IAlbumDTO> item) {
                IAlbumDTO album = item.getModelObject();
                
                PageParameters params = new PageParameters();
                params.add("id", album.getIdAlbum().toString());
                Link<Void> albumLink = new BookmarkablePageLink<Void>(ComponentIDs.ALBUMS_LINK, AlbumPage.class, params);
                albumLink.add(new Label(ComponentIDs.ALBUMS_LINK_NAME, album.getTitle()));
                item.add(albumLink);
                
                List<IArtistDTO> artists = AlbumsPanel.this.albums.get(album);
                ListView<IArtistDTO> artistsView = new ListView<IArtistDTO>(ComponentIDs.ARTISTS_VIEW, artists) {

                    @Override
                    protected void populateItem(ListItem<IArtistDTO> item) {
                        IArtistDTO artist = item.getModelObject();
                        PageParameters parameters = new PageParameters();
                        parameters.add("id", artist.getIdArtist().toString());
                        Link<Void> artistLink = new BookmarkablePageLink<Void>(ComponentIDs.ARTIST_LINK, ArtistPage.class, parameters);
                        artistLink.add(new Label(ComponentIDs.ARTIST_LINK_NAME, artist.getName()));
                        item.add(artistLink);
                    }
                };
                item.add(artistsView);
            }
        };
        this.add(albumsView);
    }
    
    /** Identifiers of added components. */
    private static final class ComponentIDs {
        /** View of songs. */
        private static final String ALBUMS_VIEW = "albumsView";
        /** Link to song detail page. */
        private static final String ALBUMS_LINK = "albumLink";
        /** Name of the song. */
        private static final String ALBUMS_LINK_NAME = "albumLinkName";
        /** View of artist for every album. */
        private static final String ARTISTS_VIEW = "artistsView";
        /** Link to artist detail page. */
        private static final String ARTIST_LINK = "artistLink";
        /** Name of the artist. */
        private static final String ARTIST_LINK_NAME = "artistLinkName";
    }
}